﻿using System.Threading;
using System.Globalization;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class ManageStadium : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.ManageStadium_PageTitle;
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("Default.aspx");
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            //this.Title = Resources.Resource.ManageStadium_Title + " - " + GND.ReturnAppTitle();

            string StadiumId;
            if (ou.TeamId == "Null")
                Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

            if (Request.QueryString["Message"] != null)
                this.MessageLabel.Text = Request.QueryString["Message"];
            
            MessagePanel.Visible = MessageLabel.Text.Length > 0;

            StadiumId = GND.ReadNivdalIntData("@UserId", "Nivdal_GetStadiumByUser", ou.UserId);

            if (StadiumId == "Null")
                Response.Redirect("~/CreatewNewStadium.aspx");

            StadiumIdHiddenField.Value = StadiumId;

            if (!IsPostBack)
            {
                this.StadiumSQLDataSource.SelectParameters.Add("StadiumId", StadiumId);
                //this.StadiumSQLDataSource.UpdateParameters.Add("StadiumId", StadiumId);
                this.UpgradeStadiumSqlDataSource.SelectParameters.Add("StadiumId", StadiumId);
                this.UnderConstructionSqlDataSource.SelectParameters.Add("StadiumId", StadiumId);
                this.StadiumNameTextBox.Text = (GND.ReadNivdalStringData("@StadiumId", "Nivdal_StadiumGetName", StadiumId));
                this.BuidCostsDataSource.SelectParameters.Add("UserId", ou.UserId);
            }

            //this.MessageLabel.Text = (GND.ReadNivdalIntData("@StadiumId", "Nivdal_StadiumShowTicketsCostBasic", StadiumId)).ToString();
            Boolean StadiumUnderConstruction = IsStadiumUnderConstruction(StadiumId);
            UpdateSeatsQuantityPanel.Visible = !StadiumUnderConstruction;
            this.UnderConstructionFormView.Visible = StadiumUnderConstruction;
        }


        public bool IsStadiumUnderConstruction(string StadiumId)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            return (GND.ReadNivdalBoolData("@StadiumId", "Nivdal_IsStadiumUnderConstruction", StadiumId));
        }


        protected void CalculateConstructionCostButton_Click(object sender, EventArgs e)
        {
            this.ShowConstructionCostLabel.ForeColor = System.Drawing.Color.Black;
            this.CalculateConstructionCostButton.Visible = true;
            this.UpradeButton.Visible = false;
            decimal MaxValue = 100000;
            bool IsInputOK = true;
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string BasicSeatsQuantity = this.BasicSeatsTextBox.Text.Trim();
            string UnderRoofQuantity = this.UnderRoofSeatsTextBox.Text.Trim();
            string VIPQuantity = this.VipSeatsTextBox.Text.Trim();
            if (BasicSeatsQuantity.Length == 0 || UnderRoofQuantity.Length == 0 || VIPQuantity.Length == 0)
            {
                this.ShowConstructionCostLabel.ForeColor = System.Drawing.Color.Red;
                this.ShowConstructionCostLabel.Text = Resources.Resource.ManageStadium_ShowConstructionCostLabelValuesIsNotInRange + " " + MaxValue;
                IsInputOK = false;
            }
            else if ((Convert.ToDecimal(BasicSeatsQuantity) > MaxValue) || (Convert.ToDecimal(UnderRoofQuantity) > MaxValue) || (Convert.ToDecimal(VIPQuantity) > MaxValue))
            {
                this.ShowConstructionCostLabel.ForeColor = System.Drawing.Color.Red;
                this.ShowConstructionCostLabel.Text = Resources.Resource.ManageStadium_ShowConstructionCostLabelValuesIsAbove + " " + MaxValue;
                IsInputOK = false;

            }
            if (IsInputOK)
            {
                this.ChangeSeatsValuesHyperLink.Visible = true;
                bool UsedDiscount = GND.ReadNivdalBoolData("TeamId", "Nivdal_StadiumDidTeamUseConstructionDiscount", ou.TeamId);
                this.ShowConstructionCostLabel.Text = (GND.ReadNivdalStringData("@Basic", "@UnderRoof", "@Vip", "Language", "UsedDiscount", "Nivdal_StadiumShowConstructionCost", BasicSeatsQuantity, UnderRoofQuantity, VIPQuantity, ou.Language, UsedDiscount));
                this.CalculateConstructionCostButton.Visible = false;
                this.UpradeButton.Visible = true;
                this.BasicSeatsTextBox.ReadOnly = true;
                this.UnderRoofSeatsTextBox.ReadOnly = true;
                this.VipSeatsTextBox.ReadOnly = true;

            }


        }
        protected void ChangeSeatsValuesClck(object sender, EventArgs e)
        {
            this.ChangeSeatsValuesHyperLink.Visible = false;
            this.ShowConstructionCostLabel.Text = "";
            this.CalculateConstructionCostButton.Visible = true;
            this.UpradeButton.Visible = false;
            this.BasicSeatsTextBox.ReadOnly = false;
            this.UnderRoofSeatsTextBox.ReadOnly = false;
            this.VipSeatsTextBox.ReadOnly = false;
        }

        protected void UpradeButton_Click(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string BasicSeatsQuantity = this.BasicSeatsTextBox.Text.Trim();
            string UnderRoofQuantity = this.UnderRoofSeatsTextBox.Text.Trim();
            string VIPQuantity = this.VipSeatsTextBox.Text.Trim();

            string Answer = GND.ReadNivdalStringData("UserId", "Basic", "UnderRoof", "Vip", "Nivdal_StadiumUpgradeExistingStadium",
                                     ou.UserId, BasicSeatsQuantity, UnderRoofQuantity, VIPQuantity);

            Response.Redirect("ManageStadium.aspx?Message=" + Answer);

        }
        protected void ChangeNameButton_Click(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            string StadiumName = this.StadiumNameTextBox.Text.Trim();
            string Answer = GND.ReadNivdalStringData("StadiumId", "Name", "Nivdal_StadiumSetName", StadiumIdHiddenField.Value, StadiumName);
            Response.Redirect("ManageStadium.aspx?Message=" + Answer);
        }

        protected string FormatDate(object Date)
        {
            DateTime d = DateTime.Parse(Date.ToString());
            return d.ToString("D");

        }
        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected string ChangeNameButtonClick()
        {
            return ("return confirm('" + Resources.Resource.ManageStadium_ChangeNameButton_ClickQuestion + "')");
        }


        protected string UpradeButtonClick()
        {
            return ("return confirm('" + Resources.Resource.ManageStadium_UpradeButtonClick_ClickQuestion + "')");
        }

    }
}